// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import store from './store'
import '@/lifeCycle'
import '@/assets/css/theme.default.css' /*主题颜色样式引入*/
import '@/assets/css/akjs.mobile.css' /* 安德鲁css样式库 */
import '@/assets/css/style.css' /* 布局样式覆盖文件 */
import '@/assets/css/vux.animate.css' /* vux动画 */
import 'vux/src/styles/1px.less';
import * as jsUtil from '@/utils/jsUtil';
import * as auth from '@/utils/auth';
import * as upgrade from '@/utils/upgrade'
import  { default as localStorage }  from '@/utils/localStorage'
import wx from 'weixin-js-sdk' //微信js sdk引入
import '@/mock'

//必要全局初始化插件
import { LoadingPlugin } from 'vux'
Vue.use(VueRouter)
Vue.use(LoadingPlugin)
import  { ConfirmPlugin } from 'vux'
Vue.use(ConfirmPlugin)
import  { AlertPlugin } from 'vux'
Vue.use(AlertPlugin)
import  { ToastPlugin } from 'vux'
Vue.use(ToastPlugin, {position: 'middle', isShowMask: true})// 默认参数
import { XProgress } from 'vux'
Vue.use(XProgress)
window.onerror = function(msg, url, line) {
  var idx = url.lastIndexOf("/");
  if(idx > -1) {
    url = url.substring(idx+1);
  }
  alert("ERROR in " + url + " (line #" + line + "): " + msg);
  return false;
};

//防止快速点击
FastClick.attach(document.body)

//是否启用生产环境
Vue.config.productionTip = true

/* eslint-disable no-new */
var appRouter = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')
export default appRouter
/*
 *微信公众号数据处理：
 * hash模式 【域名/index.html】 是保持不变的，不会像history模式一样从新请求页面，所有此处路由采用hash模式
 * */
if(jsUtil.isWeChat()){
  const openId = jsUtil.getUrlParam("openId");
  localStorage.setItem("openId",openId?openId:'');
  const token = jsUtil.getUrlParam("token");
  auth.setToken(token?token:'');
  const appId = jsUtil.getUrlParam("appId");
  localStorage.setItem("appId",appId?appId:'');
  const nonceStr = jsUtil.getUrlParam("nonceStr");
  const timestamp = jsUtil.getUrlParam("timestamp");
  const signature = jsUtil.getHashUrlParam("signature");

  //微信js sdk配置
  wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: appId, // 必填，企业号的唯一标识，此处填写企业号corpid
    timestamp: timestamp, // 必填，生成签名的时间戳
    nonceStr: nonceStr, // 必填，生成签名的随机串
    signature: signature,// 必填，签名，见附录1
    jsApiList: [ // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
      'checkJsApi',
      'getLocation',
      'chooseWXPay'
    ]
  });

  wx.ready(()=>{
    // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
    wx.getLocation({
      type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
      success: res => {
        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
        var speed = res.speed; // 速度，以米/每秒计
        var accuracy = res.accuracy; // 位置精度
        //webToast("经度：" + longitude + "，纬度：" + latitude, 8000);
      },
      cancel: res => {
        console.log('用户拒绝授权获取地理位置');
      }
    });
  });

  wx.error(res=>{
    // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
  });

}else {
 document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
 upgrade.checkUpgrade(device.platform, "建议您在WIFI环境下进行升级");
  }
  /*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
  // Application Constructor
  initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
  },

  onDeviceReady: function() {
   // this.receivedEvent('deviceready');
    //appRouter.start(App, 'app')
    //window.navigator.splashscreen.hide()
  },

  // Update DOM on a Received Event
  receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
  }
};

app.initialize();

}

